lib.use(['element', 'table', 'form','laydate','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var tablePlug = layui.tablePlug;
    var laydate=layui.laydate;
    var searchFrm = layui.Form('searchFrm');
    var gaugeSheetTable;

    //初始表格基础参数
    table.set(common.layui.table.config(tablePlug));

    searchFrm.selectRender();


    laydate.render({elem: '#gaugeDate' ,range: '至'});
    laydate.render({elem: '#estDate' ,range: '至'});


    // 渲染表格
    gaugeSheetTable = table.render({
            elem: '#gaugeSheetTable'
            , url: common.util.createUrl('/sale/gaugeSheet/list')

            , cols: [[
                {type:'checkbox'}
                ,{type: 'numbers', title: '编号' }
                ,{ field: 'entId',  hide: true}
                ,{ field: 'id',  hide: true}
                ,{ field: 'probId',  hide: true}
                ,{ title: '预订单编码', field: 'probNo' }
                ,{ title: '量尺日期', field: 'gaugeDate' }
                ,{ title: '预约日期', field: 'estDate' }
                ,{ title: '安装方式', field: 'instalModel' }
                ,{ title: '量尺面积', field: 'area' }
                ,{ title: '水平情况', field: 'level' }
                ,{ title: '订单状态', field: 'gaugeState' ,templet: function (row) { return common.dist.getValue("sale.GaugeSheet_GaugeStateEnum",row.gaugeState);}}
                ,{ field: 'remark',  hide: true}
                ,{ field: 'createUserId',  hide: true}
                ,{ field: 'createUserName',  hide: true}
                ,{ field: 'createTime',  hide: true}
                ,{ field: 'updateUserId',  hide: true}
                ,{ field: 'updateUserName',  hide: true }
                ,{ title: '修改时间', field: 'updateTime' }
            ]]
    });

    /**
     * 监听行单击事件，进入编辑事件
     */
    table.on('rowDouble(gaugeSheetTableFilter)', function(obj){
        update(obj)
    });
    /**
     * 删除
     */
    $(document).on('click','#del',function(){
        del(table.checkStatus('gaugeSheetTable'))
    });

    /**
     * 新增
     */
    $(document).on('click','#add',function(){
        var config = {
            title:'添加量尺单',
            content: 'sale/gaugeSheet/gaugeSheetForm.html'
        }
        common.util.openWindow(busCommon.windowConfig(config))
    });
    /**
     * 审核
     */
    $(document).on('click','#check',function(){
        check(table.checkStatus('gaugeSheetTable'))
    });
    /**
     * 导出
     */
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=gaugeSheetTable.config.page.pages;
        params['limit']=gaugeSheetTable.config.page.limit;
        common.util.exportWindows('/sale/gaugeSheet/export',params)
    });

    /**
     * 监听搜索条件提交事件
     */
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });

    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=$.extend(params,common.from.serializeObject('#searchFrm'))
        gaugeSheetTable.reload({where: params})
    }
   /* function searchTable(params) {
         params=common.from.serializeObject('#searchFrm');
        gaugeSheetTable.reload({where: params})
    }*/

    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("sale:gaugeSheet:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        var config = {
            title:'修改量尺单',
            content: 'sale/gaugeSheet/gaugeSheetForm.html?id=' + postData[0]
        }
        common.util.openWindow(busCommon.windowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("sale:gaugeSheet:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        if(obj.data[0].gaugeState!=1){common.util.error('[{0}] 当前订单非未审核状态,,禁止删除'.format(obj.data[0].probNo));return;}
        layer.confirm('确认删除!', function(index){
                console.log('Config Del param is', postData);
                common.ajax.post('/sale/gaugeSheet/delete', postData, function (resp) {
                    searchTable();
                    layer.close(index);
                })

         });
    }




    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("sale:gaugeSheet:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        if(obj.data[0].orderState!=1){common.util.error('[{0}] 当前订单非未审核状态,,禁止审核'.format(obj.data[0].probNo));return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/sale/gaugeSheet/check/'+postData[0].id, null, function (resp) {
                searchTable();
                layer.close(index);
                common.util.success("审核成功")
            })

        });
    }

    window.formCallBack=function () {
        searchTable();
    }
});
